package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class ExampleAdapter extends BaseListAdapter<LinkedTreeMap> {

    public ExampleAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_example_item, parent, false);
                holder.time = (TextView) convertView.findViewById(R.id.time);
                holder.branchName=(TextView) convertView.findViewById(R.id.branchName);
                holder.dutyTxt=(TextView) convertView.findViewById(R.id.dutyTxt);
                holder.dutyName=(TextView) convertView.findViewById(R.id.dutyName);
                holder.money=(TextView) convertView.findViewById(R.id.money);
                holder.status=(TextView) convertView.findViewById(R.id.status);
                holder.operate=(ImageView) convertView.findViewById(R.id.operate);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.time.setText(mDataList.get(position).get("duty_day_str")+"");
            holder.branchName.setText(mDataList.get(position).get("oper_name")+"");
            holder.dutyTxt.setText(mDataList.get(position).get("shifts_name")+"");
            holder.dutyName.setText(mDataList.get(position).get("user_name")+"");
            holder.money.setText("￥"+decimalFormat.format(mDataList.get(position).get("duty_yjje"))+"");
            holder.status.setText(Html.fromHtml(mDataList.get(position).get("duty_status_str")+""));

            holder.operate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final List<TieBean> stringZw = new ArrayList<TieBean>();
                    stringZw.add(new TieBean("查看", 0));
                    stringZw.add(new TieBean("编辑", 1));
                    stringZw.add(new TieBean("删除", 2));
                    stringZw.add(new TieBean("查看/上传单据", 3));
                    DialogUIUtils.showSheet(mContext, stringZw, "取消", Gravity.BOTTOM, true, true, new DialogUIItemListener() {
                        @Override
                        public void onItemClick(CharSequence text, int p) {
                        }

                        @Override
                        public void onBottomBtnClick() {
                        }
                    }).show();

                }
            });
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView time, branchName, dutyTxt, dutyName, money, status;
        ImageView operate;
        LinearLayout noDataRootLayout;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }


}
